package com.baitai.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.baitai.common.core.domain.entity.SysUser;
import com.baitai.common.utils.DateUtils;
import com.baitai.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baitai.system.mapper.UserPointsLogMapper;
import com.baitai.system.domain.UserPointsLog;
import com.baitai.system.service.IUserPointsLogService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分明细Service业务层处理
 * 
 * @author xx
 * @date 2024-07-21
 */
@Service
public class UserPointsLogServiceImpl implements IUserPointsLogService 
{
    @Autowired
    private UserPointsLogMapper userPointsLogMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询积分明细
     * 
     * @param id 积分明细主键
     * @return 积分明细
     */
    @Override
    public UserPointsLog selectUserPointsLogById(Long id)
    {
        return userPointsLogMapper.selectUserPointsLogById(id);
    }

    /**
     * 查询积分明细列表
     * 
     * @param userPointsLog 积分明细
     * @return 积分明细
     */
    @Override
    public List<UserPointsLog> selectUserPointsLogList(UserPointsLog userPointsLog)
    {
        return userPointsLogMapper.selectUserPointsLogList(userPointsLog);
    }

    /**
     * 新增积分明细
     * 
     * @param userPointsLog 积分明细
     * @return 结果
     */
    @Override
    public int insertUserPointsLog(UserPointsLog userPointsLog)
    {
        userPointsLog.setCreateTime(DateUtils.getNowDate());
        return userPointsLogMapper.insertUserPointsLog(userPointsLog);
    }

    /**
     * 修改积分明细
     * 
     * @param userPointsLog 积分明细
     * @return 结果
     */
    @Override
    public int updateUserPointsLog(UserPointsLog userPointsLog)
    {
        userPointsLog.setUpdateTime(DateUtils.getNowDate());
        return userPointsLogMapper.updateUserPointsLog(userPointsLog);
    }

    /**
     * 批量删除积分明细
     * 
     * @param ids 需要删除的积分明细主键
     * @return 结果
     */
    @Override
    public int deleteUserPointsLogByIds(Long[] ids)
    {
        return userPointsLogMapper.deleteUserPointsLogByIds(ids);
    }

    /**
     * 删除积分明细信息
     * 
     * @param id 积分明细主键
     * @return 结果
     */
    @Override
    public int deleteUserPointsLogById(Long id)
    {
        return userPointsLogMapper.deleteUserPointsLogById(id);
    }


    @Override
    public void gainPoints(Long userId,String pointsChange,String actionType,String actionDetail) {
        UserPointsLog userPointsLog = new UserPointsLog();
        userPointsLog.setPointsChange(pointsChange);
        userPointsLog.setActionType(actionType);
        userPointsLog.setActionDetail(actionDetail);
        SysUser user = userMapper.selectUserById(userId);
        BigDecimal afterPoints = new BigDecimal(user.getUserPoints());
        BigDecimal changePoints = new BigDecimal(userPointsLog.getPointsChange());
        userPointsLog.setAfterPoints(afterPoints.toString());
        BigDecimal beforePoints = afterPoints.add(changePoints);
        userPointsLog.setBeforePoints(beforePoints.toString());
        userPointsLog.setUserId(userId);
        userPointsLog.setChangeType("1");
        userPointsLog.setCreateTime(DateUtils.getNowDate());
        userPointsLogMapper.insertUserPointsLog(userPointsLog);
        user.setUserPoints(beforePoints.toString());
        BigDecimal afterTotalPoints = new BigDecimal(user.getTotalPoints());
        user.setTotalPoints(afterTotalPoints.add(changePoints).toString());
        userMapper.updateUser(user);
    }
    @Transactional
    @Override
    public int losePoints(Long userId,String pointsChange,String actionType,String actionDetail) {
        UserPointsLog userPointsLog = new UserPointsLog();
        userPointsLog.setPointsChange(pointsChange);
        userPointsLog.setActionType(actionType);
        userPointsLog.setActionDetail(actionDetail);
        SysUser user = userMapper.selectUserById(userId);
        BigDecimal afterPoints = new BigDecimal(user.getUserPoints());
        BigDecimal changePoints = new BigDecimal(userPointsLog.getPointsChange());
        userPointsLog.setAfterPoints(afterPoints.toString());
        BigDecimal beforePoints = afterPoints.subtract(changePoints);
        if(beforePoints.compareTo(new BigDecimal(0))>0){
            userPointsLog.setBeforePoints(beforePoints.toString());
            userPointsLog.setUserId(userId);
            userPointsLog.setChangeType("2");
            userPointsLog.setCreateTime(DateUtils.getNowDate());
            userPointsLog.setPointsChange("-"+userPointsLog.getPointsChange());
            userPointsLogMapper.insertUserPointsLog(userPointsLog);
            user.setUserPoints(beforePoints.toString());
            userMapper.updateUser(user);
            return 1;
        }
        return 0;
    }

    @Override
    public List<UserPointsLog> selectUserPointsLogList(Long userId, Date start, Date end,String actionType) {
        return userPointsLogMapper.selectUserPointsLogListByType(userId,start,end,actionType);
    }
}
